/*
@title: Spriggly Jump
@description: Spriggly Jump is a platformer game where you play as Spriggly, aiming to reach the sun by hopping on clouds. Navigate through various levels, focusing on timing and strategy to manage your limited moves while airborne. Discover unique mechanics like "light" clouds as you explore the expanses of the sky.
@author: GGGalang
@tags: []
@addedOn: 2024-06-19
First time? Check out the tutorial game:
https://sprig.hackclub.com/gallery/getting_started

*/

const player = "p"
const cloud = "c"
const sun = "s"
const lcloud = "l"
var level = 0
const moveTune = tune`
500: E4~500 + F4^500,
15500`
const resetTune = tune`
500: C4^500 + D4^500,
500: C4^500,
15000`
const nextTune = tune`
500: B4-500 + C5-500 + C4-500 + A5/500 + B5/500,
15500`
const onLight = tune`
91.46341463414635: C4^91.46341463414635,
91.46341463414635: D4^91.46341463414635,
91.46341463414635: E4^91.46341463414635 + F4^91.46341463414635,
91.46341463414635: G4^91.46341463414635,
91.46341463414635: A4^91.46341463414635 + B4^91.46341463414635,
91.46341463414635: C5^91.46341463414635 + D5^91.46341463414635 + E5~91.46341463414635 + F5~91.46341463414635 + G5~91.46341463414635,
2378.048780487805`
const onWin = tune`
81.30081300813008: B5~81.30081300813008 + C5^81.30081300813008 + E4-81.30081300813008 + D4-81.30081300813008,
81.30081300813008: G5~81.30081300813008 + D5^81.30081300813008 + E4-81.30081300813008 + D4-81.30081300813008,
81.30081300813008: A5~81.30081300813008 + B4^81.30081300813008 + D4-81.30081300813008 + E4-81.30081300813008,
81.30081300813008: F5~81.30081300813008 + A4^81.30081300813008 + D4-81.30081300813008 + E4-81.30081300813008,
81.30081300813008: B5~81.30081300813008 + C5^81.30081300813008 + D4-81.30081300813008 + E4-81.30081300813008,
81.30081300813008: G5~81.30081300813008 + D5^81.30081300813008 + D4-81.30081300813008 + E4-81.30081300813008,
81.30081300813008: A5~81.30081300813008 + B4^81.30081300813008 + D4-81.30081300813008 + E4-81.30081300813008,
81.30081300813008: F5~81.30081300813008 + A4^81.30081300813008 + D4-81.30081300813008 + E4-81.30081300813008,
81.30081300813008: B5~81.30081300813008 + C5^81.30081300813008 + D4-81.30081300813008 + E4-81.30081300813008,
81.30081300813008: G5~81.30081300813008 + D5^81.30081300813008 + E4-81.30081300813008 + D4-81.30081300813008,
81.30081300813008: A5~81.30081300813008 + B4^81.30081300813008 + E4-81.30081300813008 + D4-81.30081300813008,
81.30081300813008: F5~81.30081300813008 + A4^81.30081300813008 + E4-81.30081300813008 + D4-81.30081300813008,
81.30081300813008: B5~81.30081300813008 + C5^81.30081300813008 + E4-81.30081300813008 + D4-81.30081300813008,
81.30081300813008: G5~81.30081300813008 + D5^81.30081300813008 + E4-81.30081300813008 + D4-81.30081300813008,
81.30081300813008: A5~81.30081300813008 + B4^81.30081300813008 + E4-81.30081300813008 + D4-81.30081300813008,
81.30081300813008: F5~81.30081300813008 + A4^81.30081300813008 + E4-81.30081300813008 + D4-81.30081300813008,
81.30081300813008: B5~81.30081300813008 + C5^81.30081300813008 + E4-81.30081300813008 + D4-81.30081300813008,
81.30081300813008: G5~81.30081300813008 + D5^81.30081300813008 + E4-81.30081300813008 + D4-81.30081300813008,
81.30081300813008: A5~81.30081300813008 + B4^81.30081300813008 + E4-81.30081300813008 + D4-81.30081300813008,
81.30081300813008: F5~81.30081300813008 + A4^81.30081300813008 + E4-81.30081300813008 + D4-81.30081300813008,
81.30081300813008: B5~81.30081300813008 + C5^81.30081300813008 + E4-81.30081300813008 + D4-81.30081300813008,
81.30081300813008: G5~81.30081300813008 + D5^81.30081300813008 + E4-81.30081300813008 + D4-81.30081300813008,
81.30081300813008: A5~81.30081300813008 + B4^81.30081300813008 + E4-81.30081300813008 + D4-81.30081300813008,
81.30081300813008: F5~81.30081300813008 + A4^81.30081300813008 + E4-81.30081300813008 + D4-81.30081300813008,
81.30081300813008: B5~81.30081300813008 + C5^81.30081300813008 + E4-81.30081300813008 + D4-81.30081300813008,
81.30081300813008: G5~81.30081300813008 + D5^81.30081300813008 + E4-81.30081300813008 + D4-81.30081300813008,
81.30081300813008: A5~81.30081300813008 + B4^81.30081300813008 + E4-81.30081300813008 + D4-81.30081300813008,
81.30081300813008: F5~81.30081300813008 + A4^81.30081300813008 + E4-81.30081300813008 + D4-81.30081300813008,
81.30081300813008: B5~81.30081300813008 + C5^81.30081300813008 + E4-81.30081300813008 + D4-81.30081300813008,
81.30081300813008: G5~81.30081300813008 + D5^81.30081300813008 + E4-81.30081300813008 + D4-81.30081300813008,
81.30081300813008: A5~81.30081300813008 + B4^81.30081300813008 + E4-81.30081300813008 + D4-81.30081300813008,
81.30081300813008: F5~81.30081300813008 + A4^81.30081300813008 + E4-81.30081300813008 + D4-81.30081300813008`
const start = tune`
500: B5-500,
500: B5-500 + G5-500,
500: B5-500 + G5-500,
500: G5-500 + E5-500,
500: E5-500,
500: E5-500,
13000`

playTune(start)


setLegend(
  [ player, bitmap`
................
................
................
................
................
................
................
................
................
................
................
................
.......77.......
......7007......
......0770......
......0000......` ],
  [ cloud, bitmap`
......LLLL......
.....LLLLLL.....
....LLLLLLLL....
...LLLLLLLLLL...
..LLLLLLLLLLLL..
...7............
...7.7......7...
.....7...7..7...
.....7...7......
.........7......
...7...7........
...7...7...7....
...7.......7....
.....7.....7....
...7.7..7...7...
...7.7..7...7...`],
  [ sun, bitmap`
6......66......6
.6.....66.....6.
..666666666666..
..666666666666..
..666666666666..
..666666666666..
..666666666666..
6666666666666666
6666666666666666
..666666666666..
..666666666666..
..666666666666..
..666666666666..
..666666666666..
.6.....66.....6.
6......66......6`],
  [ lcloud, bitmap`
......7777777...
....777777777...
...7777777......
7777777.........
777777....7.....
7777....777..7..
......777.777...
.77777..777.77..
.......77..77...
...7777...77...7
..7.....77...77.
......777...77..
....77....77....
.777..7777......
.............777
7777777777.777..`]
)

const levels = [
  map`
......s  
...c...
.c.....
..c....
....c..
..p....
`,map`
..cs...  
......c
..c..c.
.......
...c...
..p....
`,map`
s.........
...c..c.c.
.....c....
.c.c...cc.
..c...c..c
....c.....
..c...c.c.
c...c.....
.c.c..c.c.
...p......`,map`
....s.....
..........
..l.......
....l.l..l
.l........
...l....l.
......l...
..l.......
.....l....
...p......`,map`
...cs.....
....c.c..l
..........
......l.c.
.c..c.....
..........
.....c....
..........
..c.......
...p..l...`,map`
...........
...........
...........
...........
.....s.....
...........
...........
...........
...........
...p.......`
]

const currentLevel = levels[level];
setMap(currentLevel);

setSolids([ player, cloud, lcloud ]);
setPushables({
  [ player ]: []
})

// inputs for player movement control
onInput("s", () => {
  getFirst(player).y += 1;
  playTune(moveTune);
});

onInput("d", () => {
  getFirst(player).x += 1;
  playTune(moveTune);
});

onInput("w", () => {
  getFirst(player).y -= 1;
  playTune(moveTune);
});

onInput("a", () => {
  getFirst(player).x -= 1;
  playTune(moveTune);
});

onInput("j", () => {
  const currentLevel = levels[level];

  setMap(currentLevel);
  playTune(resetTune);
});

var steps = 3;
var maxHeight = 5;

function checkLight(){
  if (getTile(getFirst(player).x, getFirst(player).y+1)[0].type == "l"){
    steps += 1;
    getFirst(player).y -= 1
    clearTile(getFirst(player).x, getFirst(player).y+2);
    playTune(onLight);
  }
}

afterInput(() => {
  //if there is sun on tile, next level
  if (getTile(getFirst(player).x, getFirst(player).y).length > 1) {
    console.log("triggered")
    if (getTile(getFirst(player).x, getFirst(player).y)[1].type == "s"){
      console.log("trigs")
      level += 1;
      setMap(levels[level]);
      playTune(nextTune)
      steps = 3;
      if (level > 1){
        maxHeight = 9;
      }

      if (level == 5){
        addText("You won!", { 
          x: 5,
          y: 5,
          color: color`3`
        });
        playTune(onWin);
      }
      return;
    }
  }

  //if cloud below, reset steps
  if (getTile(getFirst(player).x, getFirst(player).y+1).length != 0){
    steps = 3;
    checkLight();
  }
  
  //if not on ground and not on cloud, reset
  if (getFirst(player).y != maxHeight && getTile(getFirst(player).x, getFirst(player).y+1).length == 0) {
    steps -= 1;
  }

  //if steps are less than or equal to 0
  if (steps <= 0){
    //reset steps
    steps = 3;

    //if not on cloud
    if (getTile(getFirst(player).x, getFirst(player).y+1).length == 0){

      //while not on cloud or not on ground
      while (getFirst(player).y != maxHeight || getTile(getFirst(player).x, getFirst(player).y+1).length == 0){

        //move down one time
        if ( getTile(getFirst(player).x, getFirst(player).y+1).length == 0){
          getFirst(player).y += 1;
        } else {
          break;
        }
        if (getFirst(player).y == maxHeight) {
          break;
        }
      }
      checkLight();
    }
  }
})
